package com.tzq.bearstory;

import android.app.Application;
import android.app.PendingIntent;
import android.content.Intent;
import com.umeng.commonsdk.UMConfigure;
import com.ximalaya.ting.android.opensdk.constants.ConstantsOpenSdk;
import com.ximalaya.ting.android.opensdk.datatrasfer.CommonRequest;
import com.ximalaya.ting.android.opensdk.model.PlayableModel;
import com.ximalaya.ting.android.opensdk.model.advertis.Advertis;
import com.ximalaya.ting.android.opensdk.model.advertis.AdvertisList;
import com.ximalaya.ting.android.opensdk.player.XmPlayerManager;
import com.ximalaya.ting.android.opensdk.player.advertis.IXmAdsStatusListener;
import com.ximalaya.ting.android.opensdk.player.appnotification.NotificationColorUtils;
import com.ximalaya.ting.android.opensdk.player.appnotification.XmNotificationCreater;
import com.ximalaya.ting.android.opensdk.player.service.IXmPlayerStatusListener;
import com.ximalaya.ting.android.opensdk.player.service.XmPlayerException;
import com.ximalaya.ting.android.opensdk.util.BaseUtil;
import io.flutter.plugin.common.EventChannel;

public class MyApplication extends Application {
    public static EventChannel.EventSink eventSink;

    @Override
    public void onCreate() {
        super.onCreate();
        LogUtils.d("MyApplication-----------onCreate");
        ConstantsOpenSdk.isDebug = false;
        LogUtils.d("isMainProcess--" + BaseUtil.isMainProcess(this));

        NotificationColorUtils.isTargerSDKVersion24More = true;
        if (BaseUtil.getCurProcessName(getApplicationContext()).contains(":player")) {
            XmNotificationCreater creater = XmNotificationCreater.getInstanse(this);
            creater.setNextPendingIntent(getPendingIntent(MyPlayerReceiver.ACTION_NEXT));
            creater.setPrePendingIntent(getPendingIntent(MyPlayerReceiver.ACTION_PRE));
            creater.setStartOrPausePendingIntent(getPendingIntent(MyPlayerReceiver.ACTION_PAUSE_START));
//            creater.setClosePendingIntent(getPendingIntent(MyPlayerReceiver.ACTION_CLOSE));

            notifyPlayStatusUpdate();
        } else {
            LogUtils.d("----UM_CHANNEL=" + BuildConfig.UM_CHANNEL);
            UMConfigure.init(this, "5c4e7e35f1f5566427000795", BuildConfig.UM_CHANNEL, 0, "");

            CommonRequest.getInstanse().setAppkey("840c5ccc5d8231dcec880a274e352b0d");
            CommonRequest.getInstanse().setPackid("com.dar.bearstory");
            CommonRequest.getInstanse().init(this, "639fb0db487124649ded0aa63df25d9b");

            addXmPlayerListener();
        }
        notifyPlayStatusUpdate();
    }

    private PendingIntent getPendingIntent(String action) {
        Intent intent = new Intent(action);
        intent.setClass(getApplicationContext(), MyPlayerReceiver.class);
        return PendingIntent.getBroadcast(this, 0, intent, 0);
    }

    private void addXmPlayerListener() {
        final String type = "player";
        XmPlayerManager.getInstance(getApplicationContext()).addAdsStatusListener(new IXmAdsStatusListener() {
            @Override
            public void onStartGetAdsInfo() {
                LogUtils.d("IXmAdsStatusListener onStartGetAdsInfo");
            }

            @Override
            public void onGetAdsInfo(AdvertisList advertisList) {
                LogUtils.d("IXmAdsStatusListener onGetAdsInfo");
            }

            @Override
            public void onAdsStartBuffering() {
                LogUtils.d("IXmAdsStatusListener onAdsStartBuffering");
            }

            @Override
            public void onAdsStopBuffering() {
                LogUtils.d("IXmAdsStatusListener onAdsStopBuffering");
            }

            @Override
            public void onStartPlayAds(Advertis advertis, int i) {
                LogUtils.d("IXmAdsStatusListener onStartPlayAds");
                eventSuccess(String.format("{\"type\":\"%s\",\"status\":\"onStartPlayAds\"}", type));
            }

            @Override
            public void onCompletePlayAds() {
                LogUtils.d("IXmAdsStatusListener onCompletePlayAds");
                eventSuccess(String.format("{\"type\":\"%s\",\"status\":\"onCompletePlayAds\"}", type));
            }

            @Override
            public void onError(int i, int i1) {
                LogUtils.d("IXmAdsStatusListener onError");
            }
        });
        XmPlayerManager.getInstance(getApplicationContext()).addOnConnectedListerner(new XmPlayerManager.IConnectListener() {
            @Override
            public void onConnected() {
                LogUtils.d("IConnectListener onConnected 播放器初始化成功");
                eventSuccess(String.format("{\"type\":\"%s\",\"status\":\"onConnected\"}", type));
            }
        });
        XmPlayerManager.getInstance(getApplicationContext()).addPlayerStatusListener(new IXmPlayerStatusListener() {
            @Override
            public void onPlayStart() {
                LogUtils.d("IXmPlayerStatusListener onPlayStart");
                eventSuccess(String.format("{\"type\":\"%s\",\"status\":\"onPlayStart\"}", type));
                notifyPlayStatusUpdate();
            }

            @Override
            public void onPlayPause() {
                LogUtils.d("IXmPlayerStatusListener onPlayPause");
                eventSuccess(String.format("{\"type\":\"%s\",\"status\":\"onPlayPause\"}", type));
                notifyPlayStatusUpdate();
            }

            @Override
            public void onPlayStop() {
                LogUtils.d("IXmPlayerStatusListener onPlayStop");
                eventSuccess(String.format("{\"type\":\"%s\",\"status\":\"onPlayStop\"}", type));
                notifyPlayStatusUpdate();
            }

            @Override
            public void onSoundPlayComplete() {
                LogUtils.d("IXmPlayerStatusListener onSoundPlayComplete");
//                eventSuccess(String.format("{\"type\":\"%s\",\"status\":\"onSoundPlayComplete\"}",type));
                notifyPlayStatusUpdate();
            }

            @Override
            public void onSoundPrepared() {
                LogUtils.d("IXmPlayerStatusListener onSoundPrepared");
//                eventSuccess(String.format("{\"type\":\"%s\",\"status\":\"onSoundPrepared\"}",type));
                notifyPlayStatusUpdate();
            }

            @Override
            public void onSoundSwitch(PlayableModel playableModel, PlayableModel playableModel1) {
                LogUtils.d("IXmPlayerStatusListener onSoundSwitch");
                eventSuccess(String.format("{\"type\":\"%s\",\"status\":\"onSoundSwitch\",\"id\":%d}", type, playableModel1.getDataId()));
                notifyPlayStatusUpdate();
            }

            @Override
            public void onBufferingStart() {
//                LogUtils.d("IXmPlayerStatusListener onBufferingStart");
//                eventSuccess(String.format("{\"type\":\"%s\",\"status\":\"onBufferingStart\"}",type));
                notifyPlayStatusUpdate();
            }

            @Override
            public void onBufferingStop() {
//                LogUtils.d("IXmPlayerStatusListener onBufferingStop");
//                eventSuccess(String.format("{\"type\":\"%s\",\"status\":\"onBufferingStop\"}",type));
                notifyPlayStatusUpdate();
            }

            @Override
            public void onBufferProgress(int i) {
//                LogUtils.d("IXmPlayerStatusListener onBufferProgress  " + i);
//                eventSuccess(String.format("{\"type\":\"%s\",\"status\":\"onBufferProgress\"}",type));
            }

            @Override
            public void onPlayProgress(int i, int i1) {
//                LogUtils.d("IXmPlayerStatusListener onPlayProgress " + i + "  " + i1);
                eventSuccess(String.format("{\"type\":\"%s\",\"status\":\"onPlayProgress\",\"now\":%d,\"all\":%d}", type, i, i1));
            }

            @Override
            public boolean onError(XmPlayerException e) {
                LogUtils.e("IXmPlayerStatusListener onError", e);
                eventSuccess(String.format("{\"type\":\"%s\",\"status\":\"onError\"}", type));
                notifyPlayStatusUpdate();
                return false;
            }
        });
    }

    public void eventSuccess(String s) {
        if (eventSink == null) {
            LogUtils.e("-----eventSink==null");
            return;
        }
        eventSink.success(s);
    }

    private void notifyPlayStatusUpdate() {
//        Intent intent = MyService.getPlayIntent(getApplicationContext(), MyService.ACTION_STATUS_UPDATE);
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//            startForegroundService(intent);
//        } else {
//            startService(intent);
//        }
    }
}
